package com.qianfeng.zw.meilishuo.fragment;


import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshStaggeredGridView;
import com.handmark.pulltorefresh.library.internal.StaggeredGridView;
import com.jayqqaa12.abase.core.fragment.AFragment;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.qianfeng.zw.meilishuo.R;
import com.qianfeng.zw.meilishuo.adapter.HotSellAdapter;
import com.qianfeng.zw.meilishuo.bean.AppConn;
import com.qianfeng.zw.meilishuo.bean.TwitterGoods;
import com.qianfeng.zw.meilishuo.config.AppConstants;
import com.qianfeng.zw.meilishuo.listener.OnCompleteLinstener;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 流行
 */
@EFragment(R.layout.fragment_fashion)
public class FashionFragment extends AFragment implements OnCompleteLinstener, PullToRefreshBase.OnRefreshListener2<StaggeredGridView> {
    private HttpHandler<String> remmHandler, bannerHandler;
    /**
     * 商品列表
     */
    @ViewById(R.id.fragment_fashion_goods_staggered)
    PullToRefreshStaggeredGridView ptrStaggeredGridView;
    HotSellAdapter goodsAdapter;
    ArrayList<TwitterGoods> goods = new ArrayList<TwitterGoods>();
    private int page = 0;

    @Override
    protected void afterInject() {
        super.afterInject();
        httpHelper.setCompleteLinstener(this);
    }

    /**
     * 程序入口
     */
    @Override
    public void init() {
        goodsAdapter = new HotSellAdapter(goods, activity);
        ptrStaggeredGridView.setOnRefreshListener(this);
        StaggeredGridView staggeredGridView = ptrStaggeredGridView.getRefreshableView();
        staggeredGridView.setAdapter(goodsAdapter);
    }

    private RequestParams getParams() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("offset", String.valueOf(page));
        params.addBodyParameter("limit", "30");
        params.addBodyParameter("imei", "000000000000000");
        params.addBodyParameter("mac", "08%3A00%3A27%3A51%3A2e%3Aaa");
        params.addBodyParameter("qudaoid", "11601");
        params.addBodyParameter("access_token", "d15836232a41bb069709260e2e6d6a20");
        return params;
    }

    @Override
    protected void connect() {
        super.connect();
        httpHelper.postData(AppConstants.HOME_FASHION_POST_GOODS, getParams(), new TypeReference<AppConn<TwitterGoods>>() {
        });
    }

    @Override
    public <T> void onCompleteLinstener(List<T> data) {
        if (page == 0 && !goods.isEmpty()) {
            goods.clear();
        }
        goods.addAll((List<TwitterGoods>) data);
        goodsAdapter.notifyDataSetChanged();
        ptrStaggeredGridView.onRefreshComplete();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<StaggeredGridView> refreshView) {
        page = 0;
        connect();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<StaggeredGridView> refreshView) {
        page++;
        connect();
    }
}
